<!--
 * @Description: 系统账号
 * @Author: 阳婷
 * @Date: 2021-03-23 10:13:23
 * @LastEditTime: 2023-05-12 15:57:30
 * @LastEditors: 阳婷
-->
<template>
    <div class="sideBox">
        <div class="sideBoxInfo">
            <el-form ref="form" :model="form" label-width="140px">
                <el-form-item label="账号状态">
                    <el-radio-group v-model="form.status" @change="setStatus">
                        <el-radio :label="1">启用</el-radio>
                        <el-radio :label="0">禁用</el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item label="账号">
                    <el-input v-model="form.username" :disabled="!form.status" @change="setName"></el-input>
                </el-form-item>
                <el-form-item label="密码">
                    <el-input v-model="form.password" type="password" :disabled="!form.status" @change="setPassword">
                    </el-input>
                </el-form-item>
                <el-form-item label="确认密码">
                    <el-input v-model="form.password_confirmation" type="password" :disabled="!form.status"></el-input>
                </el-form-item>
            </el-form>
        </div>
        <div class="footerBtn">
            <el-button @click="onSubmit" v-show="form.status" type="primary" size="small">保存</el-button>
        </div>
    </div>
</template>
<script>
    import {
        deepCopy,
        isEmptyObject
    } from "@/utils/index"
    export default {
        props: ["rowData"],
        data() {
            return {
                form: {
                    id: '',
                    status: 1,
                    username: "",
                    password: "",
                    password_confirmation: ""
                }
            }
        },
        watch: {
            rowData() {
                this.clearData();
                if (!isEmptyObject(this.rowData)) {
                    this.form = deepCopy(this.rowData)
                }
            }
        },
        mounted() {
            this.clearData();
            if (!isEmptyObject(this.rowData)) {
                this.form = deepCopy(this.rowData)
            }
        },
        methods: {
            setName(name) {
                if (!name.trim()) {
                    this.$message({
                        type: "warning",
                        message: "账号不能为空！"
                    })
                } else if (!isNaN(parseInt(name))) {
                    this.$message({
                        type: "warning",
                        message: "账号不能以数字开头！"
                    })
                } else if (name.length < 6 || name.length >= 20) {
                    this.$message({
                        type: "warning",
                        message: "账号长度必须在6~20之间！"
                    })
                } else {
                    this.form.username = name
                }
            },
            setPassword(pass) {
                if (!pass.trim()) {
                    this.$message({
                        type: "warning",
                        message: "密码不能为空！"
                    })
                } else if (pass.length < 6 || pass.length >= 20) {
                    this.$message({
                        type: "warning",
                        message: "密码长度必须在6~20之间！"
                    })
                } else {
                    this.form.password = pass
                }
            },
            setStatus() {
                // debugger
                this.$ajax("put", "/api/admin", this.form, data => {
                    if (data.code == 200) {
                        if (this.form.status == 0) {
                            this.$parent.$parent.$parent.show = false
                        }
                        this.$parent.$parent.$parent.getData()
                    }
                    this.$message({
                        type: "success",
                        message: data.msg
                    })
                })
            },
            onSubmit() {
                if (this.form.status == 1) {
                    if (this.form.username !== "") {
                        if (!this.form.username.trim()) {
                            this.$message({
                                type: "warning",
                                message: "账号不能为空！"
                            })
                        } else if (!isNaN(parseInt(this.form.username))) {
                            this.$message({
                                type: "warning",
                                message: "账号不能以数字开头！"
                            })
                        } else if (this.form.username.length < 6 || this.form.username.length >= 20) {
                            this.$message({
                                type: "warning",
                                message: "账号长度必须在6~20之间！"
                            })
                        } else {
                            if (this.form.password !== "") {
                                if (!this.form.password.trim()) {
                                    this.$message({
                                        type: "warning",
                                        message: "密码不能为空！"
                                    })
                                } else if (this.form.password.length < 6 || this.form.password.length >= 20) {
                                    this.$message({
                                        type: "warning",
                                        message: "密码长度必须在6~20之间！"
                                    })
                                } else {
                                    if (this.form.password_confirmation !== "") {
                                        if (this.form.password !== this.form.password_confirmation) {
                                            this.$message({
                                                type: "warning",
                                                message: "两次密码填写不一致"
                                            })
                                        } else {
                                            this.$ajax("post", "/api/admin/password", {
                                                id: this.form.id,
                                                username: this.form.username,
                                                password: this.form.password,
                                                password_confirmation: this.form.password_confirmation
                                            }, data => {
                                                if (data.code == 200) {
                                                    this.clearData()
                                                    this.closeBox()
                                                }
                                                this.$message({
                                                    type: "success",
                                                    message: data.msg
                                                })
                                            })
                                        }
                                    } else {
                                        this.$message({
                                            type: "warning",
                                            message: "请输入确认密码"
                                        })

                                    }
                                }
                            } else {
                                this.$message({
                                    type: "warning",
                                    message: "请输入密码"
                                })
                            }
                        }
                    } else {
                        this.$message({
                            type: "warning",
                            message: "请输入账号"
                        })
                    }
                } else {
                    // this.$ajax("post", "/api/admin/password", {
                    //     id: this.form.id,
                    //     username: this.form.username,
                    //     password: this.form.password,
                    //     password_confirmation: this.form.password_confirmation
                    // }, data => {
                    //     if (data.code == 200) {
                    //         this.clearData()
                    //         this.closeBox()
                    //     }
                    //     this.$message({
                    //         type: "success",
                    //         message: data.msg
                    //     })
                    // })
                }
            },
            closeBox() {
                this.$parent.$parent.$parent.show = false
                this.$parent.$parent.$parent.getData()
            },
            clearData() {
                this.form = {
                    id: '',
                    status: 1,
                    username: "",
                    password: "",
                    password_confirmation: ""
                }
            }
        }
    }
</script>